package trastienda.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import trastienda.excepcion.DAOExcepcion;
import trastienda.excepcion.LoginExcepcion;
import trastienda.modelo.Clientes;
import trastienda.util.ConexionBD;


public class ClientesDAO extends BaseDAO {

	public Clientes insertar(Clientes vo) throws DAOExcepcion {
		String query = "INSERT INTO Clientes(dni, nomCliente,apeCliente, email, clave, fecNac) VALUES (?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
	
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getdni());
			stmt.setString(2, vo.getnomCliente());
			stmt.setString(3, vo.getapeCliente());
			stmt.setString(4, vo.getemail());
			stmt.setString(5, vo.getclave());
			stmt.setString(6, vo.getfecNac());
			

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public 	Clientes obtener(String dni) throws DAOExcepcion {
		Clientes ins = new Clientes();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select dni, nomCliente, apeCliente, email,clave, fecNac, idArmado from Clientes where dni=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, dni);
			rs = stmt.executeQuery();
			if (rs.next()) {
				
				ins.setdni(rs.getString(1));
				ins.setnomCliente(rs.getString(2));
				ins.setapeCliente(rs.getString(3));
				ins.setemail(rs.getString(4));
				ins.setclave(rs.getString(5));
				ins.setfecNac(rs.getString(6));
				
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ins;
	}
	
	public Collection<Clientes> buscarPornomCliente(String nomCliente)
			throws DAOExcepcion {
		String query = "select dni,nomCliente,apeCliente,email,clave,fecNac"
				+ " from Clientes where nombres like ?";
		Collection<Clientes> c = new ArrayList<Clientes>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nomCliente + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Clientes vo = new Clientes();
				vo.setdni(rs.getString("dni"));
				vo.setnomCliente(rs.getString("nomCliente"));
				vo.setapeCliente(rs.getString("apeCliente"));
				vo.setemail(rs.getString("email"));
				vo.setclave(rs.getString("clave"));
				vo.setfecNac(rs.getString("fecNac"));
				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	public Clientes validar(String dni, String clave)
			throws DAOExcepcion, LoginExcepcion {
		String query = "select id_usuario, nombres, paterno,"
				+ "materno, correo, direccion, telefono,"
				+ "estado from usuario where id_usuario=? and clave=?";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Clientes vo = new Clientes();
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, dni);
			stmt.setString(2, clave);
			rs = stmt.executeQuery();

			if (rs.next()) {
				vo.setdni(rs.getString("dni"));
				vo.setnomCliente(rs.getString("nomCliente"));
				vo.setapeCliente(rs.getString("apeCliente"));
				vo.setemail(rs.getString("email"));
				vo.setclave(rs.getString("clave"));
				vo.setfecNac(rs.getString("fecNac"));
			} else {
				throw new LoginExcepcion("No existe");
			}
		} catch (LoginExcepcion e) {
			System.err.println(e.getMessage());
			throw new LoginExcepcion(e.getMessage());
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

}
	
	
	

